class GameManager {
    constructor() {
        // 页面元素
        this.helpPanel = document.getElementById('helpPanel');
        this.helpBtn = document.getElementById('helpBtn');
        this.closeHelpBtn = document.getElementById('closeHelpBtn');
        this.fullscreenBtn = document.getElementById('fullscreenBtn');
        this.restartBtn = document.getElementById('restartBtn');
        this.restartGameBtn = document.getElementById('restartGameBtn');
        this.gameMessage = document.getElementById('gameMessage');
        this.body = document.body;
        this.gameTitle = document.querySelector('.game-title');
        this.scoreElement = document.getElementById('score');
        this.highScoreElement = document.getElementById('highScore');
        this.controlList = document.querySelector('.control-list');

        // 绑定事件处理器
        this.bindEvents();

        // 添加全屏变化事件监听
        document.addEventListener('fullscreenchange', () => {
            if (!document.fullscreenElement) {
                this.body.classList.remove('fullscreen');
            }
        });
    }

    bindEvents() {
        // 帮助面板
        this.helpBtn.addEventListener('click', () => this.toggleHelpPanel());
        this.closeHelpBtn.addEventListener('click', () => this.toggleHelpPanel());

        // 全屏
        this.fullscreenBtn.addEventListener('click', () => this.toggleFullscreen());

        // 重启游戏
        this.restartBtn.addEventListener('click', () => this.restartGame());
        this.restartGameBtn.addEventListener('click', () => this.restartGame());

        // 键盘快捷键
        document.addEventListener('keydown', (e) => this.handleKeyPress(e));
    }

    toggleHelpPanel() {
        this.helpPanel.classList.toggle('active');
    }

    async toggleFullscreen() {
        try {
            if (!document.fullscreenElement) {
                await this.body.requestFullscreen();
                this.body.classList.add('fullscreen');
            } else {
                if (document.exitFullscreen) {
                    await document.exitFullscreen();
                }
            }
        } catch (err) {
            console.error('Error attempting to toggle fullscreen:', err.message);
        }
    }

    handleKeyPress(event) {
        switch(event.key.toLowerCase()) {
            case 'h': // 帮助面板
                this.toggleHelpPanel();
                break;
            case 'f': // 全屏
                this.toggleFullscreen();
                break;
            case 'escape':
                if (document.fullscreenElement) {
                    document.exitFullscreen();
                    this.body.classList.remove('fullscreen');
                } else if (this.helpPanel.classList.contains('active')) {
                    this.toggleHelpPanel();
                }
                break;
            case 'r': // 重启
                this.restartGame();
                break;
        }
    }

    setGameTitle(title) {
        this.gameTitle.textContent = title;
    }

    updateScore(score) {
        this.scoreElement.textContent = score;
        
        // 更新最高分
        const highScore = Math.max(parseInt(this.highScoreElement.textContent), score);
        this.highScoreElement.textContent = highScore;
    }

    setControls(controls) {
        this.controlList.innerHTML = controls.map(control => `
            <div class="control-item">
                <span class="key">${control.key}</span>
                <span class="action">${control.action}</span>
            </div>
        `).join('');
    }

    showGameMessage(title, score) {
        this.gameMessage.querySelector('h2').textContent = title;
        this.gameMessage.querySelector('.final-score').textContent = score;
        this.gameMessage.classList.add('active');
    }

    hideGameMessage() {
        this.gameMessage.classList.remove('active');
    }

    restartGame() {
        // 这个方法将由具体的游戏实现来覆盖
        this.hideGameMessage();
        console.log('Restart game');
    }
}

// 创建游戏管理器实例
const gameManager = new GameManager();

// 示例：设置游戏标题和控制说明
gameManager.setGameTitle('示例游戏');
gameManager.setControls([
    { key: '↑', action: '向上' },
    { key: '↓', action: '向下' },
    { key: '←', action: '向左' },
    { key: '→', action: '向右' },
    { key: 'R', action: '重新开始' },
    { key: 'F', action: '全屏' },
    { key: 'H', action: '帮助' }
]);
